package tuSecure;

import java.io.IOException;
import java.util.Date;
import java.util.Random;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class SignGuestbookServlet extends HttpServlet {
    private static final Logger log = Logger.getLogger(SignGuestbookServlet.class.getName());

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();

        String content = req.getParameter("content");
        
        Random random = new Random();
        
        int rand = random.nextInt(4);

 
        String symbol =getSymbol(rand);
        String contentt=getContent(rand);
        String phone= this.getPhone(rand);
        String comment= getComment(rand);
        String lat= ""+this.getRandomLat(rand);
        String longt=""+this.getRandomLong(rand);
        String date=this.getDate(rand);
        String type =this.getType(rand);
        
        Alert alrt = new Alert(symbol,contentt,type,phone,comment,longt,lat,date);
        PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            pm.makePersistent(alrt);
        } finally {
            pm.close();
        }
        
        resp.sendRedirect("/guestbook.jsp");
    }
    
    public String getContent(int rand)
    {
    	String val;
    	switch(rand)
    	{
	    	case 0: val="Jamie Hawbaker"; break;
			case 1:val="Erik Outten"; break;
			case 2:val="Clayton Wantz"; break;
			case 3:val="Marcie Whitehill"; break;
			default:val="Cody Heber"; break;
    	}
    	return val;
    }
    
    public String getType(int rand)
    {
    	String val;
    	switch(rand)
    	{
	    	case 0: val="Fire Emergergency"; break;
			case 1:val="Medical Emergency"; break;
			case 2:val="Minor Problem"; break;
			case 3:val="Locked out"; break;
			default:val="Life threatening Emergency"; break;
    	}
    	return val;
    }
    
    public String getSymbol(int rand)
    {
    	String val;
    	switch(rand)
    	{
	    	case 0: val="*"; break;
			case 1:val="+"; break;
			case 2:val="*"; break;
			case 3:val="+"; break;
			default:val="*"; break;
    	}
    	return val;
    }
    
    public String getDate(int rand)
    {
    	String val;
    	switch(rand)
    	{//MM/dd/yyyy HH:mm
	    	case 0: val="12/06/2010 04:45"; break;
			case 1:val="12/06/2010 4:30"; break;
			case 2:val="12/16/2010 18:45"; break;
			case 3:val="12/06/2010 09:45"; break;
			default:val="12/06/2010 07:45"; break;
    	}
    	return val;
    }
    
    public String getPhone(int rand)
    {
    	String val;
    	switch(rand)
    	{
	    	case 0: val="215-224-8979"; break;
			case 1:val="215-223-1234"; break;
			case 2:val="732-355-9195"; break;
			case 3:val="574-123-0834"; break;
			default:val="123-345-5678"; break;
    	}
    	return val;
    }
    
    public String getComment(int rand)
    {
    	String val;
    	switch(rand)
    	{
	    	case 0: val="Im on Fire"; break;
			case 1:val="I'm Bleeding"; break;
			case 2:val="I'm Running"; break;
			case 3:val="I'm Locked Out"; break;
			default:val="I'm Getting Mugged"; break;
    	}
    	return val;
    }
    
    public double getRandomLong(int rand)
    {
    	double val;
    	
    	switch(rand)
    	{	
    		case 0: val=-75.158322; break;
    		case 1:val=-75.15551805496216; break;
    		case 2:val=-75.150239; break;
    		case 3:val=-75.153201; break;
    		default:val=-75.158522; break;
    	
    	}
    	return val;
    }
    public double getRandomLat(int rand)
    {
    	double val;
    	
    	switch(rand)
    	{	
    		case 0: val=39.979065; break;
    		case 1:val=39.980695817470775; break;
    		case 2:val= 39.978032; break;
    		case 3:val= 39.983261; break;
    		default:val=39.98474; break;
    	
    	}
    	
    	
    	return val;
    }
}